package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsFootprint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
@Service
public class DtsAdminServiceImpl implements DtsAdminService{
    @Autowired
    private DtsAdminMapper adminMapper;
    @Override
    public DtsAdmin findAdmin(String username) {

        DtsAdmin dtsAdmin = adminMapper.selectOne(new LambdaQueryWrapper<DtsAdmin>()
                .eq(DtsAdmin::getUsername, username)
                .eq(DtsAdmin::getDeleted, false));
        return dtsAdmin;
    }

    @Override
    public List<DtsAdmin> findAll() {

        List<DtsAdmin> dtsAdminsList = adminMapper.selectList(new QueryWrapper<DtsAdmin>());
            return dtsAdminsList;

    }
    //分页管理员列表
    @Override
    public List<DtsAdmin> findAdmins(int page, int limit, String sort, String order, String username) {
        QueryWrapper<DtsAdmin> queryWrapper=new QueryWrapper<>();
        //模糊查询用户名--排序方式
        queryWrapper.like(!StringUtils.isEmpty(username),"username",username);
        queryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort);
        //分页
        Page<DtsAdmin> dtsAdminPage = adminMapper.selectPage(new Page<DtsAdmin>(page, limit), queryWrapper);
        List<DtsAdmin> dtsAdminList = dtsAdminPage.getRecords();
        return dtsAdminList;
    }

    @Override
    public int count() {
        return adminMapper.selectCount(null);
    }
    //创建管理员用户
    @Override
    public void create(DtsAdmin admin, String add) {
        admin.setAddTime(new Date());
        admin.setLastLoginTime(new Date());
        admin.setLastLoginIp(add);
        admin.setUpdateTime(new Date());
        adminMapper.insert(admin);


    }
}
